<template>
	<view>
		<view class="tabs">
			<view class="tabs-item flex--aj-center" v-for="(item,index) in categoryList" :key="index">
				<view class="item-content flex--aj-center" @click="tabClick(index)"
					:class="{'item-content-no': index !== tabCurrent,'item-content-active': index === tabCurrent}">
					{{item.name}}
					<view class="item-img" v-if="index === tabCurrent">
						<image src="../../static/icon/order_tab.png" mode=""></image>
					</view>
				</view>
			</view>
		</view>

		<view class="list">
			<template v-if="list.length > 0">
				<view class="complist">
					<view class="index_sale">
						<view class="free-WaterfallFlow">
							<view class="flex-wrap" v-for="(item,index) in list" :key="index" @click="jump(item.id)">
								<view class="page-column">
									<image mode="widthFix" :src="item.image"></image>
								</view>
							</view>
						</view>
					</view>
				</view>
			</template>
			<template v-else>
				<view class="empty" v-if="show">
					<view class="empty-text">
						暂时没有数据
					</view>
				</view>
			</template>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				categoryList: [],
				tabCurrent: 0,
				list: [],
				page: 1,
				pageSize: 10,
				last_page: 1,
				scrollLeft: 0,
				show: false
			}
		},
		onLoad() {
			this.getCategory()
		},
		onReachBottom() {
			let _this = this;
			// 已经是最后一页
			if (_this.page >= _this.last_page) {
				return false;
			}

			_this.page++

			_this.getArticle()
		},
		methods: {
			getCategory() {
				let _this = this
				uni.showLoading({
					title: '加载中...'
				})
				_this.$api.get('meet/article.category/index', {}).then(res => {
					if (res.code == 1) {
						_this.categoryList = res.data.list

						_this.getArticle()
					} else {
						_this.toast(res.msg)
					}
				}, fail => {})
			},
			getArticle() {
				let _this = this

				_this.$api.get('meet/article.article/index', {
					page: _this.page,
					page_size: _this.pageSize,
					category_id: _this.categoryList[_this.tabCurrent].id
				}).then(res => {
					uni.hideLoading()
					if (res.code == 1) {
						let list = _this.list.concat(res.data.data)

						_this.list = list

						_this.last_page = res.data.last_page

						_this.show = true
					} else {
						_this.toast(res.msg)
					}
				}, fail => {})
			},
			tabClick(index) {
				if (this.tabCurrent === index) {
					return false
				}

				this.tabCurrent = index
				this.show = false
				this.page = 1
				this.last_page = 1
				this.list = []
				uni.showLoading({
					title: '加载中...'
				})
				this.getArticle()
			},
			jump(id) {
				uni.navigateTo({
					url: '/pages/article/detail?id=' + id
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #F6F6F6;
		padding-bottom: 80rpx;
	}

	.flex--aj-center {
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.flex--justify-between {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.flex--align-start {
		display: flex;
		align-items: start;
	}

	.tabs {
		background-color: #FFFFFF;
		display: flex;
		align-items: center;
		overflow-x: auto;
		white-space: nowrap;

		.tabs-item {
			// width: 16.6%;
			margin-right: 10rpx;

			.item-content {
				// width: 120rpx;
				min-width: 120rpx;
				height: 92rpx;
				position: relative;

				.item-img {
					position: absolute;
					top: 20%;
					left: 0;
					width: 100%;
					text-align: center;

					image {
						width: 120rpx;
						height: 65rpx;
					}
				}
			}

			.item-content-no {
				font-weight: 400;
				font-size: 32rpx;
				color: #8A8A8A;
			}

			.item-content-active {
				font-weight: 800;
				font-size: 36rpx;
				color: #000000;
			}
		}
	}

	.list {
		padding-top: 10px;
		padding-bottom: 20px;
	}

	.empty {
		width: 100%;
		text-align: center;
		margin-top: 40%;

		image {
			width: 200rpx;
			height: 200rpx;
		}

		.empty-text {
			font-size: 32rpx;
			color: #999999;
			margin-top: 80rpx;
		}
	}

	.complist {
		margin-top: 10px;
		padding: 0 15px;

		.index_sale {
			.free-WaterfallFlow {
				width: 100%;
				// column-count: 2;

				/*column-count 属性规定元素应该被分隔的列数：*/
				.flex-wrap {
					display: inline-block;
					width: 50%;
					// margin-right: 2%;
					text-align: left;
					vertical-align: top;
					margin-bottom: 24rpx;

					.page-column {
						width: 100%;

						image {
							width: 97%;
							margin: 0 auto;
							border-radius: 14rpx;
						}
					}
				}

				.flex-wrap:nth-child(2n) {
					text-align: right;
				}
			}
		}
	}
</style>